package com.brillilab.service.logic.message;

import com.alibaba.fastjson.JSONObject;
import com.brillilab.common.redis.RedisCache;
import com.brillilab.common.utils.ValidatorUtil;
import com.brillilab.domain.vo.message.AliPushVo;
import com.brillilab.domain.vo.message.IMAccountVo;
import com.brillilab.domain.vo.message.SmsSendVo;
import com.brillilab.service.constant.ServiceConstant;
import com.brillilab.service.core.aliyun.IAliYunPushService;
import com.brillilab.service.core.aliyun.IAliYunSmsService;
import com.brillilab.service.core.email.IEmailService;
import com.brillilab.service.module.netease.NeteaseIm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Map;

@Slf4j
@Service
public class MessageLogic {

    @Autowired
    private IAliYunPushService aliYunPushService;
    @Autowired
    private IAliYunSmsService aliYunSmsService;
    @Autowired
    private NeteaseIm neteaseIm;
    @Autowired
    private IEmailService emailService;
    @Autowired
	private RedisCache redisCache;
    
    public boolean sendVerifyCode(String mobile) {
        return aliYunSmsService.sendVerifyCode(mobile);
    }

    public boolean sendVerifyCode(String mobile ,Integer maxCount) {
        return aliYunSmsService.sendVerifyCode(mobile,maxCount);
    }

    public boolean checkVerifyCode(String mobile,String code) {
    	String verifyCode=redisCache.get(ServiceConstant.sms_verify_code + mobile);
    	Assert.isTrue(StringUtils.isNotBlank(verifyCode), "短信验证码已过期，请重新获取");
    	Assert.isTrue(verifyCode.equals(code.trim()), "请输入正确的短信验证码");
    	redisCache.delete(ServiceConstant.sms_verify_code + mobile);
    	return true;
    }

    public boolean sendEmpowerCode(String mobile,Long labId) {
        return aliYunSmsService.sendEmpowerCode(mobile,labId);
    }

    
    public boolean sendSms(SmsSendVo smsSendVo) {
        return aliYunSmsService.sendSms(smsSendVo);
    }

    public boolean sendSmsContent(String mobile, String templateCode, Map<String, Object> paramMap){
        return aliYunSmsService.sendSmsContent(mobile,templateCode,paramMap);
    }
    
    public boolean aliYunPush(AliPushVo aliPushVo) {
        try {
            aliYunPushService.aliYunPush(aliPushVo);
            return true;
        } catch (Exception e) {
            log.error("阿里云消息推送接口异常：",e);
            return false;
        }
    }

    /**
     * 网易云信账号
     * @param accid
     * @param name
     * @return
     */
    public IMAccountVo createIMUser(String accid,String name) {
    	
    	Assert.isTrue(StringUtils.isNotBlank(accid) && StringUtils.isNotBlank(name), "参数错误");
    	IMAccountVo imAccount= null;
        try {
        	JSONObject rs=neteaseIm.createUser(accid,name,null);
            String code=rs.getString("code");
            log.info("result:{}",rs.toString());
            Assert.isTrue(code.equals("200"), "网易云信用户创建异常");
           
            JSONObject info=rs.getJSONObject("info");
            imAccount=new IMAccountVo();
            imAccount.setAccid(info.getString("accid"));
            imAccount.setName(info.getString("name"));
            imAccount.setToken(info.getString("token"));
            
        } catch (Exception e) {
            log.error("网易云信用户创建异常:",e);
        }
        return imAccount;
    }

    /**
     * 发送邮箱验证码
     *
     * @param email
     * @return
     */
    public boolean sendEmailValidateCode(String email) {
        Assert.isTrue(ValidatorUtil.isEmail(email), "邮箱格式错误");
        return emailService.sendValidateCode(email);

    }

    /**
     * 校验邮箱验证码
     *
     * @param email
     * @param validateCode
     * @return
     */
    
    public boolean checkEmailVerifyCode(String email,String validateCode) {

        String code=redisCache.get(ServiceConstant.email_verify_code + email);
        Assert.isTrue(code!=null, "邮箱验证码已过期，请重新获取！");
        
        if(code.equals(validateCode.trim())){
            redisCache.delete(ServiceConstant.email_verify_code + email);
            return true;
        }
        return false;
    }

    /**
     * 发送邮件
     * @param title 标题
     * @param sendHtml 内容
     * @param receiveEmail 接手人邮箱
     * @return
     */
    public boolean sendEmail(String title, String sendHtml,String receiveEmail) {
        return emailService.getEmailSender().sendHtmlEmail(title,sendHtml,receiveEmail);
    }

}
